$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'ClassDiaryTable/Select',
        datatype: "json",
        colModel: [
            { label: '日期', name: 'diaryPostTime',index:'diary_post_time', width: 35,key: true, sortable:false },
            // { label: '学年', name: 'courseAcademicYear',index:'course_academic_year', width: 40 },
            // { label: '学期', name: 'courseAcademicTerm',index:'course_academic_term', width: 40 },
            // { label: '教学周', name: 'courseWhichWeek',index:'course_which_week', width: 40 },
            // { label: '星期', name: 'courseWhichDay',index:'course_which_day', width: 40 },

            { label: '教师姓名', name: 'courseTeacherName',index:'course_teacher_name', width: 30, sortable:false },
            { label: '工号', name: 'teacherId',index:'course_teacher_name', width: 40, sortable:false },
            { label: '所属院系', name: 'teacherCollege',index:'teacher_college', width: 35 , sortable:false},
             { label: '授课名称', name: 'courseName', index:'course_name',width: 50, sortable:false },
            { label: '节次', name: 'courseWhichIndex',index:'course_which_index', width: 30, sortable:false },

            { label: '授课班级', name: 'className', index:'class_name',width: 50 , sortable:false},
             { label: '到达时间', name: 'arriveTime',index:'arrive_time', width: 35, sortable:false },
             { label: '离开时间', name: 'leaveTime',index:'leave_time', width: 35 , sortable:false},
             { label: '教授时长', name: 'teachLength', index:'teach_length',width: 40, sortable:false },
            { label: '出勤', name: 'isAttendanceState',index:'leave_time', width: 40, sortable:false },
            { label: '布置作业', name: 'isHomeworkState',index:'leave_time', width: 40, sortable:false },
            { label: '考勤', name: 'isAttcheckState',index:'leave_time', width: 40, sortable:false },
            { label: '是否相符', name: 'isMatchContent',index:'leave_time', width: 40, sortable:false },
            { label: '授课类型', name: 'teachingTypeStr',index:'leave_time', width: 40, sortable:false },
            { label: '授课内容', name: '', width: 30, sortable: false, formatter: function(value, options, row){

                    var r='<a class="table-btn1 detail-btn" onclick="showTeachContentAfter(\''+row.teachContentBefore+'\',\''+row.teachContentAfter+'\')">&nbsp;详情</a>'

                    return r;
                }}

        ],
        viewrecords: true,
        height:'100%',
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "scroll" });
        },
        ondblClickRow:function(rowid){
            var rowData = $("#jqGrid").jqGrid('getRowData',rowid);
            var rowName =$("#jqGrid").jqGrid('getGridParam','colNames');
            var s="";
            var i=2;
            for(var key in rowData){
                s=s+rowName[i++]+':'+rowData[key]+'<br> ';
            }
           // alert(s);
        }
    });
    $("#exportExcel").attr('href',baseURL+'ClassDiaryTable/export?token='+token);
    $.ajax({
                 url: baseURL + "/college/getAllcollege",
                 data: {},
                 success: function (r) {
                     $("#College").append("<option value=''>请选择</option>");
                     for (i = 0; i < r.list.length; i++) {
                         var tname = r.list[i].collegeName;
                         var tid = r.list[i].collegeName;
                         $("#College").append("<option value='" + tid + "'>" + tname + "</option>");
                     }
                 }
    });
});
function  showTeachContentAfter(teachContentBefore,teachContentAfter) {
    if(teachContentBefore==null)
    {
        teachContentBefore=""
    }
    alert("授课内容：<br>    "+teachContentBefore+"<br>教学内容：<br>    "+teachContentAfter);

}
function setDate(id) {
    var Nowdate=new Date();
    M=Number(Nowdate.getMonth())+1;
    var today=Nowdate.getFullYear()+"-"+M+"-"+Nowdate.getDate();
    if(id=='1'){
        $("#startDate").val(today);
        $("#endDate").val(today);
    }else if(id=='2'){
        $("#startDate").val(showWeekFirstDay());
        $("#endDate").val(today);
    }if(id=='3'){
        $("#startDate").val(showMonthFirstDay());
        $("#endDate").val(today);
    }
    vm.reload();
}
function showWeekFirstDay()
{
    var Nowdate=new Date();
    var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);
    M=Number(WeekFirstDay.getMonth())+1
    return WeekFirstDay.getFullYear()+"-"+M+"-"+WeekFirstDay.getDate();
}
function showWeekLastDay()
{
    var Nowdate=new Date();
    var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);
    var WeekLastDay=new Date((WeekFirstDay/1000+6*86400)*1000);
    M=Number(WeekLastDay.getMonth())+1
    return WeekLastDay.getFullYear()+"-"+M+"-"+WeekLastDay.getDate();
}
function showMonthFirstDay()
{
    var Nowdate=new Date();
    var MonthFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth(),1);
    M=Number(MonthFirstDay.getMonth())+1
    return MonthFirstDay.getFullYear()+"-"+M+"-"+MonthFirstDay.getDate();
}
function showMonthLastDay()
{
    var Nowdate=new Date();
    var MonthNextFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth()+1,1);
    var MonthLastDay=new Date(MonthNextFirstDay-86400000);
    M=Number(MonthLastDay.getMonth())+1
    return MonthLastDay.getFullYear()+"-"+M+"-"+MonthLastDay.getDate();
}

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            key: null,
            startDate:null,
            endDate:null
        },
        showList: true,
        title: null,
        ClassDiary: {},
        diaryPostTime: null,
        term: {}
    },
    methods: {
        query: function () {
            $("#exportExcel").attr('href',baseURL+'ClassDiaryTable/export?token='+token+"&teacherName="+vm.q.key);
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.ClassDiary = {};
        },
        update: function () {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            $.get(baseURL + "ClassDiaryTable/Info/"+id, function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.ClassDiary = r.ClassDiary;
                vm.diaryPostTime = r.ClassDiary.diaryPostTime;
            });
        },
        del: function () {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "ClassDiary/Delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function () {
            if(vm.validator()){
                return ;
            }

            var url = vm.ClassDiary.diaryPostTime == vm.diaryPostTime ? "ClassDiaryTable/Update" : "ClassDiaryTable/Insert";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.ClassDiary),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{ 'teacherName': vm.q.key,
                    'startDate': $("#startDate").val(),
                    'endDate': $("#endDate").val(),
                    'college': $("#College").val(),
                    'profession':$("#Profession").val(),
                    'class': $("#Class").val()
                },
                page:page
            }).trigger("reloadGrid");
        },
        validator: function () {
            if(isBlank(vm.ClassDiary.diaryPostTime)){
                alert("考评时间不能为空");
                return true;
            }
            if(isBlank(vm.ClassDiary.courseTeacherName)){
                alert("教师姓名不能为空");
                return true;
            }
            if(isBlank(vm.ClassDiary.courseName)){
                alert("课程名称不能为空");
                return true;
            }
            if(isBlank(vm.ClassDiary.arriveTime)){
                alert("到达时间不能为空");
                return true;
            }
            if(isBlank(vm.ClassDiary.leaveTime)){
                alert("离开时间不能为空");
                return true;
            }
            if(isBlank(vm.ClassDiary.teachLength)){
                alert("教授时长不能为空");
                return true;
            }
            }
        }
});